import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/routes/Routes.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';

import 'StartWorkController.dart';

class StartWorkPage extends StatelessWidget {
  final logic = Get.put(StartWorkController());
  final FocusNode focusNode1 = FocusNode();
  final FocusNode focusNode2 = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(USER),
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              SubTitleBar("进站", '下一步', () {
                if(logic.lotEntity == null){
                  Get.snackbar('提示', '未获取到批次信息');
                  return;
                }
                if(logic.eqpController.text.isEmpty){
                  Get.snackbar('提示', '请扫描设备号');
                  return;
                }
                Get.toNamed(
                    (logic.lotEntity?.TRACK_IN_WIZARD == 'XNAssemblyTrackIn'
                        ? Routes.StartWorkNextOne
                        : Routes.StartWorkNextDB),
                    arguments: logic.lotEntity);
              }),
              TitleEditText(
                "批次ID:",
                "请扫描批次ID",
                    (value) {
                  logic.lotInfo(value);
                  focusNode1.unfocus();
                  FocusScope.of(Get.context!).requestFocus(focusNode2);
                },
                focusNode: focusNode1,
              ),
              GetBuilder<StartWorkController>(builder: (controller) {
                return TitleEditText(
                  "设备ID:",
                  "请扫描设备ID",
                      (value) {
                    if(logic.lotEntity == null){
                        Get.snackbar('提示', '请先扫描批次号');
                        logic.eqpController.text = '';
                        logic.update([logic.eqpId]);
                        return;
                    }
                    logic.lotEntity?.eQUIPMENTID = value;
                  },
                  focusNode: focusNode2,controller: logic.eqpController,);
              },id: logic.eqpId,),
              GetBuilder<StartWorkController>(
                builder: (logic) =>
                    Container(
                      child: Column(
                        children: [
                          TextView(
                              "批次数量:",
                              logic.lotEntity?.mAINQTY.toString() ?? '0'),
                          TextView("批次状态:", logic.lotEntity?.sTATE ?? ''),
                          TextView("当前工步:", (logic.lotEntity?.sTEPNAME ?? '') + '/' + (logic.lotEntity?.sTEPDESC ?? '')),
                        ],
                      ),
                    ),
                id: logic.lotInfoId,
              ),
            ],
          ),
        ));
  }
}
